﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class WorkFeedback : EFViewCmes
    {
        public WorkFeedback(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
 //                   this.fdType.IsEnabled = false;
 //                   this.fdEquipmentId.IsEnabled = false;
                    this.more.Visibility = Visibility.Collapsed;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
//                    this.Commit.Visibility = Visibility.Collapsed;
                    this.more.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertPanel.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }
        public WorkFeedback()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
        /// <summary>
        /// 控制显示异常详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void more_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            if (!e.OldValue.Equals(""))
            {
                if (e.NewValue.ToString().Equals("1"))
                {
                    this.InsertGridMore.Visibility = Visibility.Visible;
                    this.fdDegree.eRegex = "^.+$";
                    this.fdDegree.eValidateErrorPrompt = "输入不能为空";
                    this.fdExceptionDescription.eRegex = ".{1,125}$";
                    this.fdExceptionDescription.eValidateErrorPrompt = "输入1~125个字符";
                }
                else
                {
                    this.InsertGridMore.Visibility = Visibility.Collapsed;
                    this.fdDegree.eRegex = "^.*$";
                    this.fdDegree.eValidateErrorPrompt = "";
                    this.fdExceptionDescription.eRegex = "^.{0,125}$";
                    this.fdExceptionDescription.eValidateErrorPrompt = "输入0~125个字符";
                }
            }
        }
        /// <summary>
        /// 设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (!(picker.ItemObject == null || "".Equals(picker.ItemObject)))
            {
                this.fdEquipmentId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                this.fdEquipmentCode.Value = picker.ItemObject.GetType().GetProperty("fdCode").GetValue(picker.ItemObject, null);
                this.fdUseDepartment.Value = picker.ItemObject.GetType().GetProperty("fdDepartment").GetValue(picker.ItemObject, null);
                this.fdUseDepartmentId.Value = picker.ItemObject.GetType().GetProperty("fdDepartmentId").GetValue(picker.ItemObject, null);
            }
        }

        private void fdType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (this.fdType.Value.Equals("大修") || this.fdType.Value.Equals("维修"))
            {
                this.more.IsReadOnly = true;
                this.more.Value = "0";
                this.fdDegree.Value = "";
                this.fdExceptionDescription.Value = "";
                this.InsertGridMore.Visibility = Visibility.Collapsed;
            }else{
                this.more.IsReadOnly = false;
            }
        }
    }
}
